package com.example.yoho.mvp.view.fragment;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.yoho.R;
import com.example.yoho.utils.MyCustom;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CommunityFragment extends Fragment implements View.OnClickListener {


    private ImageView openDrawers;
    private ImageView imgBuy;
    private CommonTabLayout ctl;
    private List<CustomTabEntity> list = new ArrayList<>();
    private StrlloFragment strlloFragment = new StrlloFragment();
    private MyCommunityFragment myCommunityFragment = new MyCommunityFragment();
    private MyShowFragment showFragment = new MyShowFragment();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_community, null);
        openDrawers = (ImageView) view.findViewById(R.id.open_drawers);
        imgBuy = (ImageView) view.findViewById(R.id.img_buy);
        ctl = (CommonTabLayout) view.findViewById(R.id.ctl);
        openDrawers.setOnClickListener(this);
        initView();
        return view;

    }

    private void initView() {

        getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.community_fl,strlloFragment)
                .add(R.id.community_fl,myCommunityFragment)
                .add(R.id.community_fl,showFragment)
                .show(strlloFragment)
                .hide(myCommunityFragment)
                .hide(showFragment)
                .commit();
        MyViewPages();


    }



    private void MyViewPages() {
        list.add(new MyCustom("逛", 0, 0));
        list.add(new MyCustom("社区", 0, 0));
        list.add(new MyCustom("Show", 0, 0));

        ctl.setTabData((ArrayList<CustomTabEntity>) list);

        ctl.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position) {
                    case 0:
                        imgBuy.setImageResource(R.drawable.heart);
                        getActivity().getSupportFragmentManager().beginTransaction()
                                .show(strlloFragment)
                                .hide(showFragment)
                                .hide(myCommunityFragment)
                                .commit();


                        break;
                    case 1:
                        imgBuy.setImageResource(R.drawable.x);
                        getActivity().getSupportFragmentManager().beginTransaction()
                                .hide(strlloFragment)
                                .hide(showFragment)
                                .show(myCommunityFragment)
                                .commit();
                        break;
                    case 2:
                        imgBuy.setImageResource(R.drawable.picture);
                        getActivity().getSupportFragmentManager().beginTransaction()
                                .hide(strlloFragment)
                                .show(showFragment)
                                .hide(myCommunityFragment)
                                .commit();
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.open_drawers:
                View views = LayoutInflater.from(getContext()).inflate(R.layout.silde_item, null);
                SlidingMenu slidingMenu = new SlidingMenu(getContext());
                slidingMenu.attachToActivity(getActivity(), SlidingMenu.SLIDING_CONTENT);//绑定到指定的Context
                slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                slidingMenu.setBehindOffset(250);
                slidingMenu.setMenu(views);
                slidingMenu.setMode(SlidingMenu.LEFT);
                slidingMenu.showMenu();
                break;

        }

    }
}

























































































































































































































































